$(function () {

    $("#bt_insertCusLevel").click(function () {

        var name = $('input[name="name"]').val();
        var minScore = $('input[name="minScore"]').val();
        var maxScore = $('input[name="maxScore"]').val();

        if (name.trim().length > 0 && minScore.trim().length > 0 && maxScore.trim().length > 0) {
            $.ajax({
                type: "post",
                url: "/crm/customer/cusLevel/",
                data: {
                    "name": name,
                    "minScore": minScore,
                    "maxScore": maxScore
                },
                success: function (data) {
                    if (data == "SUCCESS") {
                        $("#msg").css({color: "green"});
                        $("#msg").html("添加成功！");
                        window.setTimeout(function () {
                            $(location).attr('href', '/crm/customer/cusLevelList');
                        }, 1200);
                    } else {
                        $("#msg").css({color: "red"});
                        $("#msg").html("添加失败！");
                    }
                }
            });
        } else {
            $("#msg").html("请完善所有必填项！");
        }
    })

    $(".bt_update").click(function () {

        var id = $(this).prev().val();
        var name = $(this).parent().prev().prev().prev().text();
        var minScore = $(this).parent().prev().prev().text();
        var maxScore = $(this).parent().prev().text();

        $("#update-id").val(id);
        $("#update-name").val(name);
        $("#update-minScore").val(minScore);
        $("#update-maxScore").val(maxScore);

        $("#bt_updateCusLevel").click(function () {

            var uid = $("#update-id").val();
            var uname = $("#update-name").val();
            var umin = $("#update-minScore").val();
            var umax = $("#update-maxScore").val();

            if (uid.trim().length > 0 && uname.trim().length > 0 && umin.trim().length > 0 && umax.trim().length > 0) {
                $.ajax({
                    type: "post",
                    url: "/crm/customer/updateCusLevel",
                    data: {
                        "id": uid,
                        "name": uname,
                        "minScore": umin,
                        "maxScore": umax
                    },
                    success: function (data) {
                        if (data == "SUCCESS") {
                            $("#msg_update").css({color: "green"});
                            $("#msg_update").html("修改成功！");
                            window.setTimeout(function () {
                                $(location).attr('href', '/crm/customer/cusLevelList');
                            }, 1200);
                        } else {
                            $("#msg_update").css({color: "red"});
                            $("#msg_update").html("修改失败！");
                        }
                    }
                });
            } else {
                $("#msg").html("请完善所有必填项！");
            }
        });
    })

    $(".bt_del").click(function () {
        var id = $(this).prev().prev().val();
        $("#bt_confirm").click(function () {
            $.ajax({
                type: "post",
                url: "/crm/customer/deleteCusLevel",
                data: {
                    "id": id
                },
                success: function (data) {
                    if (data == "SUCCESS") {
                        $("#msg_del").css({color: "green"});
                        $("#msg_del").html("删除成功！");
                        window.setTimeout(function () {
                            $(location).attr('href', '/crm/customer/cusLevelList');
                        }, 2000);
                    } else {
                        $("#msg_del").css({color: "red"})
                        $("#msg_del").html("删除失败！");
                        window.setTimeout(function () {
                            $(location).attr('href', '/crm/customer/cusLevelList');
                        }, 1200);
                    }
                }
            })
        })
    });
});